package cn.csc.framework.mq.util;


import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;

import java.util.Map;

/**
 * @Author：云隐
 * @Date：2023/3/20 18:39
 */
@Slf4j
@RequiredArgsConstructor
public class RocketSendMsgUtils {

    private final StreamBridge streamBridge;

    /**
     *
     * @param channel 信道，binding name
     * @param headers 消息头信息，若要顺序消费，则需设置MessageConst.PROPERTY_ORIGIN_MESSAGE_ID，并配置messageQueueSelector
     * @param msg 消息
     * @return
     */
    public boolean sendMsg(String channel,Map<String, Object> headers, Object msg) {
        //延迟消费 延迟10秒
        MessageHeaders messageHeaders = new MessageHeaders(headers);
        Message<Object> message = MessageBuilder.withPayload(msg).copyHeadersIfAbsent(messageHeaders).build();
        return streamBridge.send(channel, message);
    }
}
